package discovery

import (
	"github.com/hyperway/pkg/component"
	"github.com/hyperway/pkg/logger"
	"sync"
	"time"
)

type MockDiscover int

func NewMockDiscover() component.Discover {
	return new(MockDiscover)
}

func (m *MockDiscover) Once(p *component.ServerPool, waiter *sync.WaitGroup) {
	metas := load()
	x := len(metas)
	for i, meta := range metas {
		if i == x-1 {
			println("last")
		}
		server := p.GetServer(meta.ServerId)
		server.RegisterMvc(meta)
	}
	waiter.Done()
}

func (m *MockDiscover) Ongoing(p *component.ServerPool) {

	service := component.NewServiceMeta(
		"GET:/test/once:v1",
		"localhost:2859/demo/hello",
		"http",
		"",
		"GET",
		nil,
	)

	time.Sleep(30 * time.Second)
	metadata := component.NewEndpointMeta(
		"GET:/test/ongoing:v1",
		"DEMO",
		"GET",
		"/test/ongoing",
		"v1",
		"echo",
		"gateway",
		service,
	)
	server := p.GetServer(metadata.ServerId)
	server.RegisterMvc(metadata)
	logger.Infow("discover ongoing", "name", server.WebListener.Name())
}
